package com.dhcc.sdc.busi.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.dhcc.cdc.busi.dao.CdcEpisodeMapper;
import com.dhcc.cdc.busi.entity.CdcEpisodeEntity;
import com.dhcc.cdc.busi.service.ICdcEpisodeService;
import com.dhcc.cdc.sys.cache.config.CdcConfigCache;
import com.dhcc.cdc.sys.service.IConfigurationService;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.security.ShiroKit;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.modules.system.entity.User;
import com.dhcc.sdc.busi.entity.SdcBusiQcMrlistEntity;
import com.dhcc.sdc.busi.entity.SdcBusiQcitemExecResultEntity;
import com.dhcc.sdc.busi.service.ISdcBusiQcMrlistService;
import com.dhcc.sdc.busi.service.ISdcBusiQcfitemExecResultService;
import com.dhcc.sdc.busi.service.ISdcQcFormShowService;
import com.dhcc.sds.base.entity.SdsQcentityEntity;
import com.dhcc.sds.base.entity.SdsQcentityRuleEntity;
import com.dhcc.sds.base.entity.SdsQcfitemDicEntity;
import com.dhcc.sds.base.entity.SdsQcformItemEntity;
import com.dhcc.sds.base.service.*;
import com.dhcc.sds.busi.dto.SdsQcFormItemDto;
import com.dhcc.sds.busi.entity.SdsQcfitemExecresultEntity;
import com.dhcc.sds.busi.entity.SdsQcincaseEntity;
import com.dhcc.sds.busi.entity.SdsQcincaseOperlogEntity;
import com.dhcc.sds.busi.service.ISdsQcFormShowService;
import com.dhcc.sds.busi.service.ISdsQcfitemExecresultService;
import com.dhcc.sds.busi.service.ISdsQcincaseDataService;
import com.dhcc.sds.busi.service.ISdsQcincaseService;
import com.dhcc.sds.util.DateUtil;
import com.dhcc.sds.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.script.ScriptException;
import java.io.UnsupportedEncodingException;
import java.util.*;

/**
 * @version 1.0 DHCC
 * @Description: 表单填报页面
 * @author:yanghangwei
 * @date:2024年03月01日09:58:22
 * @Copyright: (c) DHCC All rights reserved.
 */
@Controller
@RequestMapping("${sys.backendPath}/sdc/sdcqcformshow")
public class SdcQcFormShowController extends BaseController {

    private String PREFIX = "/backend/sds/sdsqcformshow/";

    @Autowired
    private ISdcBusiQcfitemExecResultService sdcBusiQcfitemExecResultService;
    @Autowired
    private ISdcQcFormShowService sdcQcFormShowService;

    @RequestMapping("/getItemInfo/{mrListId}")
    @ResponseBody
    public Object getItemInfo(@PathVariable Long mrListId){
        List<SdsQcFormItemDto> itemList = new ArrayList<>();
        try {
            String initMrList = CdcConfigCache.me().getValueByKey("SDS", "SDCBusiInitMrList","N");
            if ("Y".equals(initMrList)) {
                // 根据数据源配置自动取值
                sdcBusiQcfitemExecResultService.execItemResult(mrListId);
            }
            itemList =  sdcQcFormShowService.getBusiItemResult(mrListId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return itemList;
    }

    /**
     * 查询表单填报状态
     */
    @RequestMapping("/mrState/{mrListId}")
    @ResponseBody
    public Object getCurrStat( @PathVariable Long mrListId){
        return "P";
    }
    /**
     * 保存病种项目结果值
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public Object save(@RequestParam Map<String, Object> params) {
        Boolean result;
        // 获取当前登录人姓名(设置为公共属性)
        String execUser = params.get("execUser").toString();
        Long mrListId  = Long.valueOf(params.get("mrListId").toString());
        Long itemId = Long.valueOf(params.get("itemId").toString());
        String itemVal ="";
        if (CommonUtil.isNotEmpty(params.get("itemVal"))) {
            itemVal = params.get("itemVal").toString();
        }
        // 删除值中的特殊字符
        itemVal = StrUtil.clearSpecialChar(itemVal);
        Date execDate = new Date();
        Wrapper<SdcBusiQcitemExecResultEntity> wrapper = new EntityWrapper<>();
        wrapper.eq("in_case_id", mrListId).eq("form_item_id", itemId);
        SdcBusiQcitemExecResultEntity execEntity = sdcBusiQcfitemExecResultService.selectOne(wrapper);
        if (CommonUtil.isEmpty(execEntity)) {
            execEntity = new SdcBusiQcitemExecResultEntity();
            execEntity.setInCaseId(mrListId); // 入组id
            execEntity.setFormItemId(itemId); //项目id
            execEntity.setItemResult(itemVal); //项目结果值
            execEntity.setExecUser(execUser); // 执行人
            execEntity.setExecDate(execDate); // 执行日期
            execEntity.setExecTime(execDate); // 执行时间
            execEntity.setExecType("H");
            // 新增
            result = execEntity.insert();
        } else {
            execEntity.setItemResult(itemVal); //项目结果值
            execEntity.setExecUser(execUser); // 执行人
            execEntity.setExecDate(execDate); // 执行日期
            execEntity.setExecTime(execDate); // 执行时间
            // 更新
            result = execEntity.updateAllColumnById();
        }
        if (result) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}
